# coding=utf-8
from resource_management import *
from resource_management.core import shell
from resource_management.core.logger import Logger
from resource_management.core.resources.system import Execute, File
from resource_management.libraries.functions import format
from resource_management.libraries.functions.check_process_status import check_process_status


class Master(Script):
    def install(self, env):
        import params
        env.set_params(params)

        Execute(format(
            "sh {service_packagedir}/files/downfiles.sh {war_url} {servlet_server_tar_url} {work_dir} {servlet_server_port}"),
            logoutput=True)

        # self.configure(env)
        '''
        if use psutil .check it status
        '''

    def configure(self, env, upgrade_type=None, config_dir=None):
        import params
        Logger.info("ddse-config tomcat port:" + params.servlet_server_port)

    # To stop the service, use the linux service stop command and pipe output to log file
    def stop(self, env):
        import params
        import status_params
        env.set_params(params)

        cmd = format("lsof -i :{servlet_server_port}|grep -v 'PID' ") + "| awk 'NR==1 {print $2}'"
        code, output = shell.call(cmd)
        if output:
            Execute("kill -9 " + output + " >/dev/null 2>&1", logoutput=True)

        File(status_params.webapp_pid,
             action="delete"
             )

    # To start the service, use the linux service start command and pipe output to log file
    def start(self, env):
        import params
        import status_params
        env.set_params(params)
        # 用lsof端口号查找PID 会出现 修改端口号后 重新部署,原来的tomcat关不掉,故用目录
        # cmd = format("lsof -i :{servlet_server_port}|grep -v 'PID' ") + "| awk 'NR==1 {print $2}'"
        cmd = format("ps aux | grep {work_dir}| grep tomcat") + "| awk 'NR==1 {print $2}'"
        code, output = shell.call(cmd)
        if output:
            Execute("kill -9 " + output + " >/dev/null 2>&1", logoutput=True)

        Execute(format("sh {work_dir}/apache-tomcat-8.5.13/bin/startup.sh"), logoutput=True)

        # if first_setup
        import time
        time.sleep(5)

        cmd = format("lsof -i :{servlet_server_port}|grep -v 'PID' ") + "| awk 'NR==1 {print $2}'"
        code, output = shell.call(cmd)

        # create pid file
        Directory(status_params.webapp_pid_dir,
                  create_parents=True,
                  owner='root',
                  group='root'
                  )
        File(status_params.webapp_pid,
             content=output + "\n"
             )
        import os
        if os.path.exists(status_params.webapp_pid) and output:
            Logger.info("tomcat is started.")

    # To get status of the, use the linux service status command
    def status(self, env):
        import status_params
        env.set_params(status_params)
        check_process_status(status_params.webapp_pid)

    def check(self, env):
        import params
        import status_params
        env.set_params(status_params)
        env.set_params(params)
        cmd = format("lsof -i:{servlet_server_port} | grep -v 'PID' ") + "| awk 'NR==1 {print $2}'"
        code, output = shell.call(cmd)
        Logger.info(format("execute cmd result:{code},{output}"))
        check_process_status(status_params.webapp_pid)


if __name__ == "__main__":
    Master().execute()
